package com.magic233.controller;

import com.magic233.entity.Orders;
import com.magic233.entity.Users;
import com.magic233.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * ClassName: WorkerController
 * Description:
 * date: 2021/5/30 16:18
 * author: zouyuan
 */

@RestController
@RequestMapping("/worker")
public class WorkerController {
    @Autowired
    private OrdersService ordersService;

    @RequestMapping("/confirmOrder")
    public Object confirmOrder(HttpServletRequest request, Long id) {
        System.out.println("维修人员确认接单");
        if (id == null) {
            System.out.println("参数为空");
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int re = ordersService.acceptOrder(user, id);
        return re;
    }

    @RequestMapping("/cancelOrder")
    public Object cancelOrder(HttpServletRequest request, Long id, String workerReason) {
        System.out.println("维修人员申请取消工单");
        if (workerReason == null || id == null || "".equals(workerReason)) {
            System.out.println("参数为空");
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int re = ordersService.cancelOrder(user, id, workerReason);
        return re;
    }

    @RequestMapping("/completeOrder")
    public Object completeOrder(HttpServletRequest request, Long id, String consumable, String imgsfileids2) {
        System.out.println("维修人员确认完成工单");
        if (id == null || consumable == null || "".equals(consumable)) {
            System.out.println("参数为空");
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int re = ordersService.workerCompleteOrder(user, id, consumable, imgsfileids2);
        return re;
    }

    @RequestMapping("/selectOrders")
    public Object selectOrders(HttpServletRequest request, String status, String type, Long startTime, Long endTime, Integer pageNum, Integer pageSize) {
        System.out.println("维修人员查询工单");
        Users user = (Users) request.getAttribute("user");
        Orders order = new Orders();
        order.setWid(user.getId());
        if (status == null || "".equals(status)) {
            order.setStatus("全部状态");
        } else {
            order.setStatus(status);
        }
        if (type == null || "".equals(type)) {
            order.setType("全部类别");
        } else {
            order.setType(type);
        }
        order.setSelectStartTime(startTime);
        order.setSelectEndTime(endTime);
        Map<Object, Object> map = ordersService.selectOrders(order, pageNum, pageSize);
        return map;
    }

    @RequestMapping("/setWorkernote")
    public Object setWorkernote(HttpServletRequest request, Long id, String workernote) {
        if (id == null || workernote == null || "".equals(workernote)) {
            System.out.println("参数为空");
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int i = ordersService.setWorkernote(user, id, workernote);
        return i;
    }

    @RequestMapping("/mark")
    public Object mark(HttpServletRequest request, Long[] ids) {
        if (ids == null) {
            System.out.println("参数为空");
            return 0;
        } else {
            Users user = (Users) request.getAttribute("user");
            boolean re = ordersService.mark(user, ids);
            if (re) {
                return 1;
            } else {
                return 0;
            }
        }
    }


    @RequestMapping("/cancelMark")
    public Object cancelMark(HttpServletRequest request, Long[] ids) {
        if (ids == null) {
            System.out.println("参数为空");
            return 0;
        } else {
            Users user = (Users) request.getAttribute("user");
            boolean re = ordersService.cancelMark(user, ids);
            if (re) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}
